import CreateCook from '@/views/Main/CreateCook.vue'
import InformationForm from '@/views/Main/InformationForm.vue'
import Search from '@/views/Main/Search.vue'
import UserCenter from '@/views/Main/UserCenter.vue'
import Login from '@/views/Login/Login.vue'
import { createRouter, createWebHistory } from 'vue-router'
import { useLoginStore } from '@/stores/login'
import Layout from '@/views/Main/Layout.vue'
import MyRecipe from '@/views/UserCenter/MyRecipe.vue'
import MyFocus from '@/views/UserCenter/MyFocus.vue'
import ShowRecipe from '@/views/Main/ShowRecipe.vue'
import OtherCenter from '@/views/Main/OtherCenter.vue'
import MyCollect from '@/views/UserCenter/MyCollect.vue'
import axios from 'axios'
import OtherRecipe from '@/views/OtherCenter/OtherRecipe.vue'
import OtherFocus from '@/views/OtherCenter/OtherFocus.vue'
import OtherCollect from '@/views/OtherCenter/OtherCollect.vue'
import MyInnerCollect from '@/views/UserCenter/MyInnerCollect.vue'

import Chat from '@/views/Chat/Chat.vue'
import Dialog from '@/views/Chat/Dialog.vue'
import Follower from '@/views/Chat/Follower.vue'
import Fan from '@/views/Chat/Fan.vue'
import ChatPage from '@/views/Chat/ChatPage.vue'
import FanMsg from '@/views/Chat/FanMsg.vue'
import FollowerMsg from '@/views/Chat/FollowerMsg.vue'
import { useWebSocketStore } from '@/stores/websocket'
import PersonInformation from '@/views/UserCenter/PersonInformation.vue'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/layout',
      component: Layout,
      meta: { requiresAuth: true }//给前置路由守卫判断用的，后面的同理
    },
    {
      path: '/chat',
      component: Chat,
      meta: { requiresAuth: true },//给前置路由守卫判断用的，后面的同理
      children: [
        {
          name: 'chatpage',
          path: 'chatpage',
          component: ChatPage,
          meta: { requiresAuth: true }
        },
        {
          name: 'fanmsg',
          path: 'fanmsg',
          component: FanMsg,
          meta: { requiresAuth: true }
        },
        {
          name: 'followermsg',
          path: 'followermsg',
          component: FollowerMsg,
          meta: { requiresAuth: true }
        },

      ]
    },
    {
      path: '/search',
      component: Search,
      meta: { requiresAuth: true },
    },
    {
      path: '/usercenter',
      component: UserCenter,
      meta: { requiresAuth: true },
      children: [

        {
          name: 'personinformation',
          path: 'personinformation',
          component: PersonInformation,
          meta: { requiresAuth: true },

        },
        {
          name: 'myrecipe',
          path: 'myrecipe',
          component: MyRecipe,
          meta: { requiresAuth: true }
        },
        {
          name: 'myfocus',
          path: 'myfocus',
          component: MyFocus,
          meta: { requiresAuth: true }
        },
        {
          path: 'updateinformation',
          component: InformationForm,
          name: 'informationform',
          meta: { requiresAuth: true }
        },
        {
          path: 'mycollect',
          name: 'mycollect',
          component: MyCollect,
          meta: { requiresAuth: true },
        },
        {
          path: 'myinnercollect',
          name: 'myinnercollect',
          component: MyInnerCollect,
          meta: { requiresAuth: true }
        },
      ]
    },
    {
      path: '/createcook',
      component: CreateCook,
      meta: { requiresAuth: true }
    },
    {
      path: '/showrecipe',
      component: ShowRecipe,
      props(route) {
        return route.query
      },
      meta: { requiresAuth: true }
    },
    {
      path: '/othercenter',
      component: OtherCenter,
      meta: { requiresAuth: true },
      props(route) {
        return route.query
      },
      children: [
        {
          path: 'otherrecipe',
          name: 'otherrecipe',
          component: OtherRecipe,
          meta: { requiresAuth: true }
        },
        {
          path: 'otherfocus',
          name: 'otherfocus',
          component: OtherFocus,
          meta: { requiresAuth: true }
        },
        {
          path: 'othercollect',
          name: 'othercollect',
          component: OtherCollect,
          meta: { requiresAuth: true }
        },
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: Login
    },



    {//重定向
      path: '/',
      redirect: '/login'//只能是一级路由默认打开界面
    },
  ],
})

// 全局前置守卫
router.beforeEach(async (to, from, next) => {
  // 先向后端判断是否登录，拿到code进行判断
  await axios({
    url: '/api/user/checkLogin',
    method: 'post'
  }).then(res => {
    // 如果没登录则为false,登录为true
    let isLoggedIn = res.data.code == '1010' ? false : true
    if (!isLoggedIn && to.path !== '/login') {
      next('/login')
    }
    // 其他情况正常放行
    else {
      next()
    }
  })
})
export default router
